package me.zhengjie.open.utils;

import org.apache.xerces.impl.xs.SchemaSymbols;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class PomeloHttpUtils {
    private static final String BASE_URL = "https://api.pomelo-cloud.com/api/";
    private static final String UID = "291";
    public static final String SECRET_KEY = "d976d2119e59aecddb5012915d9252bc";

    // 生成 MD5 签名
    private static String generateSign(String t) throws Exception {
        String input = UID + "1" + SECRET_KEY;
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] hash = md.digest(input.getBytes());
        StringBuilder hexString = new StringBuilder();
        for (byte b : hash) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString().toLowerCase();
    }

    // 发送 GET 请求
    private static String sendGetRequest(String endpoint, Map<String, String> params) throws Exception {
        String queryParams = params.entrySet().stream()
                .map(entry -> entry.getKey() + "=" + entry.getValue())
                .collect(Collectors.joining("&"));

        URL url = new URL(BASE_URL + endpoint + "?" + queryParams);
        System.out.println(url.toString());
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/x-www-form-urlencoded");

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        String s = response.toString();
        System.out.println(s);
        return s;
    }

    // 获取用户余额
    public static String getUserBalance(Map<String, String> params) throws Exception {
        String t = params.get("t");
        params.put("uid", UID);
        params.put("sign", generateSign(t));
        return sendGetRequest("getUser", params);
    }

    // 申请电话号码
    public static String getPhoneNumber(String country,String iid) throws Exception {
        Map<String, String> params = new HashMap<>();
        String t = String.valueOf(System.currentTimeMillis() / 1000);
        params.put("t","1");
        params.put("uid", UID);
        params.put("sign", generateSign(t));
        params.put("country",country);
        params.put("service_id",iid);
        for (String key : params.keySet()){
            System.out.println("k:  " +key+"  v:  "+params.get(key));
        }
        return sendGetRequest("getPhone", params);
    } 

    // 获取短信
    public static String getSms(Map<String, String> params) throws Exception {
        String t = String.valueOf(System.currentTimeMillis() / 1000);
        params.put("t", "1");
        params.put("uid", UID);
        params.put("sign", generateSign(t));
        return sendGetRequest("getSms", params);
    }

    // 释放电话号码
    public static String releasePhone(String orderId) throws Exception {
        Map<String, String> params = new HashMap<>();
        String t = String.valueOf(System.currentTimeMillis() / 1000);
        params.put("t", "1");
        params.put("uid", UID);
        params.put("sign", generateSign(t));
        params.put("orderId",orderId);
        return sendGetRequest("releasePhone", params);
    }

    // 获取用户产品列表
    public static String getUserProductList(Map<String, String> params) throws Exception {
        String t = params.get("t");
        params.put("uid", UID);
        params.put("sign", generateSign(t));
        return sendGetRequest("getUserProduct", params);
    }

    public static void main(String[] args) throws Exception{
//        System.out.println( String.valueOf(System.currentTimeMillis() / 1000));
        String input = "22" +"1672925840" + "14c06834431d8d85273cda4336ac100f";
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] hash = md.digest(input.getBytes());
        StringBuilder hexString = new StringBuilder();
        for (byte b : hash) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        System.out.println(hexString.toString().toLowerCase());
    }
}
